﻿using EventBusSharp;
using System.Net.Http.Headers;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.Test1();
            program.Test2();
            Console.ReadKey();
        }

        /// <summary>
        /// 发布与订阅字符串
        /// </summary>

        public void Test1()
        {
            //订阅,消息名：TestEvent自己定义,发布跟订阅一致就行
            EventBus.Instance.Subscribe(this, "TestEvent", (data) =>
            {
                Console.WriteLine(data);
            });


            //发布消息
            EventBus.Instance.Publish("TestEvent", "Hello World1");
            //发布消息
            EventBus.Instance.Publish("TestEvent", "Hello World2");
            //发布消息
            EventBus.Instance.Publish("TestEvent", "Hello World3");

            //取消订阅,订阅和取消对应必须一致
            EventBus.Instance.UnSubscribe(this, "TestEvent");

            EventBus.Instance.Publish("TestEvent", "Hello World4");
            EventBus.Instance.Publish("TestEvent", "Hello World5");
        }


        /// <summary>
        /// 发布与订阅对象
        /// </summary>
        public void Test2()
        {
            EventBus.Instance.Subscribe<MyEvent>(this, (ev) =>
            {
                Console.WriteLine(ev.Data);
            });

            //发布消息
            EventBus.Instance.Publish<MyEvent>(new MyEvent { Data = "HelloWorld1" });
            //发布消息
            EventBus.Instance.Publish<MyEvent>(new MyEvent { Data = "HelloWorld2" });
            //发布消息
            EventBus.Instance.Publish<MyEvent>(new MyEvent { Data = "HelloWorld3" });

            //取消订阅,订阅和取消对应必须一致
            EventBus.Instance.UnSubscribe<MyEvent>(this);

            EventBus.Instance.Publish<MyEvent>(new MyEvent { Data = "HelloWorld4" });
            EventBus.Instance.Publish<MyEvent>(new MyEvent { Data = "HelloWorld5" });
        }
    }

    public class MyEvent : IEvent
    {
        public string Data { get; set; }
    }
}
